package com.example.ovulationperiodtracker.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.example.ovulationperiodtracker.Allactivity.WebActivity;
import com.example.ovulationperiodtracker.R;
import com.example.ovulationperiodtracker.databinding.DialogYsBinding;
import com.example.ovulationperiodtracker.utils.Constants;


public class YsDialog extends Dialog {

    private OnClickYesNoListener onClickYesNoListener;

    public YsDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DialogYsBinding binding = DialogYsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();

        // 获取屏幕宽度
        int screenWidth = getContext().getResources().getDisplayMetrics().widthPixels;
        // 设置 Dialog 宽度为屏幕宽度的 80%
        layoutParams.width = (int) (screenWidth * 0.85);
        window.setAttributes(layoutParams);
        window.setBackgroundDrawableResource(R.drawable.bg_dialog_ys);

        setCanceledOnTouchOutside(false);

        binding.privacyPolicyTextView.setMainText("我已阅读并同意《隐私协议》和《服务协议》")
                .addProtocol("《隐私协议》", "privacy")
                .addProtocol("《服务协议》", "terms");

        binding.privacyPolicyTextView.setOnProtocolClickListener(new PrivacyPolicyTextView.OnProtocolClickListener() {
            @Override
            public void onProtocolClick(String tag) {
                switch (tag) {
                    case "privacy":
                        WebActivity.startActivity(getContext(), "隐私协议", Constants.YSXY_URL);
                        break;
                    case "terms":
                        WebActivity.startActivity(getContext(), "服务协议", Constants.YHXY_URL);
                        break;
                }
            }
        });


        binding.tvNo.setOnClickListener(v -> {
            if (onClickYesNoListener != null) {
                onClickYesNoListener.onClickNo();
            }
            dismiss();
        });

        binding.tvYes.setOnClickListener(v -> {
            if (onClickYesNoListener != null) {
                onClickYesNoListener.onClickYes();
            }
            dismiss();
        });
    }

    public YsDialog setOnClickYesNoListener(OnClickYesNoListener onClickYesNoListener) {
        this.onClickYesNoListener = onClickYesNoListener;
        return this;
    }

    public interface OnClickYesNoListener {
        void onClickYes();

        void onClickNo();
    }
}